/**
 * 面积量算
 */
import { handler } from './mapClear'
// 多边形实例
let polygonInstance
let polygonTransparent
// 多边形点数组
let polygonPositions
// 事件柄
let screenSpaceEventHandler

const inputAction = function() {
  polygonInstance = new Cesium.GeometryInstance({
    geometry: new Cesium.PolygonGeometry({
      polygonHierarchy: new Cesium.PolygonHierarchy(polygonPositions),
      perPositionHeight: true
    }),
    attributes: {
      color: new Cesium.ColorGeometryInstanceAttribute(1, 165 / 255, 0, 0.3)
    }
  })
  polygonTransparent = window.viewer.scene.primitives.add(
    new Cesium.Primitive({
      geometryInstances: polygonInstance,
      appearance: new Cesium.PerInstanceColorAppearance({
        renderState: {
          depthTest: true
        }
      })
    })
  )
  polygonInstance.geometry._polygonHierarchy.positions = polygonPositions
  polygonTransparent.show = true
}
export default () => {
  const viewer = window.viewer
  // const scene = viewer.scene
  const Cesium = window.Cesium
  if (!viewer || !Cesium) {
    return
  }
  // 清除高度距离面积的测量
  if (handler.measureHeight) {
    handler.measureHeight.deactivate()
  }
  if (handler.measureDistance) {
    handler.measureDistance.deactivate()
  }
  if (handler.measureArea) {
    handler.measureArea.deactivate()
    handler.measureArea.clear()
    removeInputAction()
    clearPolygon()
  }
  handler.measureArea = new Cesium.MeasureHandler(
    viewer,
    Cesium.MeasureMode.Area,
    Cesium.ClampMode.Space
  )
  handler.measureArea.measureEvt.addEventListener(area)
  // 激活事件，监听当前事件以获取处理器的状态
  handler.measureArea.activeEvt.addEventListener(activeFun)
  handler.measureArea.activate()

  function area(result) {
    const mj = Number(result.area)
    const area =
      mj > 1000000 ? (mj / 1000000).toFixed(2) + 'km²' : mj.toFixed(2) + '㎡'
    handler.measureArea.areaLabel.text = '面积:' + area

    // 隐藏原始的面
    handler.measureArea.polygon.show = false

    polygonPositions = result.positions
  }

  setInputAction()

  function activeFun(isActive) {
    if (isActive === true) {
      viewer.enableCursorStyle = false
      viewer._element.style.cursor = 'default'
    } else {
      viewer.enableCursorStyle = true
    }
  }
  return handler
}

/** 清除第二次添加的不被遮挡的面
 *
 */
export function clearPolygon() {
  const viewer = window.viewer

  if (polygonTransparent) {
    viewer.scene.primitives.remove(polygonTransparent)
  }
  polygonTransparent = undefined
}
export function setInputAction() {
  screenSpaceEventHandler = new Cesium.ScreenSpaceEventHandler(
    window.viewer.scene.canvas
  )
  screenSpaceEventHandler.setInputAction(
    inputAction,
    Cesium.ScreenSpaceEventType.RIGHT_CLICK
  )
}
export function removeInputAction() {
  screenSpaceEventHandler &&
    screenSpaceEventHandler.removeInputAction(
      inputAction,
      Cesium.ScreenSpaceEventType.RIGHT_CLICK
    )
  screenSpaceEventHandler &&
    !screenSpaceEventHandler.isDestroyed() &&
    screenSpaceEventHandler.destroy()
  screenSpaceEventHandler = null
}
